﻿using System;
using System.Collections.Generic;
using System.Text;
using Antu.WebControls;
using EluZSProvider;
using Antu.Core.Services;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using Antu.Business.DmpExcel;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZhengShouZBBPersonControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        Button btn_Insert,Button1;
        TextBox txt_yf;
        //Repeater Repeater1;
        DataList DataList1;
        public int count;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            DataList1 = (DataList)FindControl("DataList1");
            if (!Page.IsPostBack)
            {
                Bind();
            }
            InitControl();
            base.OnLoad(e);

        }

       

        protected void InitControl()
        {
            txt_yf = (TextBox)FindControl("txt_yf");
            btn_Insert = (Button)FindControl("btn_Insert");
            btn_Insert.Click += new EventHandler(btn_Insert_Click);
            Button1 = (Button)FindControl("Button1");
            Button1.Click += new EventHandler(Button1_Click);
        }

       

        protected void Bind()
        {
            if (Page.Request["id"]!=null)
            {
                gettable("null", Page.Request["id"]);
            }
            else
            {
                string sql = "Select * From zs_zrb Where Name Like '%" + System.DateTime.Now.Year + "%' And Name Like '%" + System.DateTime.Now.Month + "%'";
                DataTable dt = _EluZSDataProvider.GetTable(sql);
                if (dt.Rows.Count>0)
                {
                    gettable("null", dt.Rows[0]["id"].ToString());
                }
            }
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
//            string sqlwhere = string.Empty;
//            if (Page.Request["ID"] != null)
//            {
//                sqlwhere += "and z.id = '" + Page.Request["ID"] + "'";
//            }
//            else
//            {
//                sqlwhere += "And z.name Like '%" + DateTime.Now.Year + "%' And z.name Like '%" + DateTime.Now.Month + "%'";
//            }
//            string sql = @"Select z.Id,z.name 值班表名,序列 日期 ,p.Name 小组人员,xznumber 小组人员电话, atname 安图人员, atnumber 安图人员电话,备注 From zs_zrb z,zs_zrb_person p
//Where z.id = p.zid "+sqlwhere+@"             
//Order By 序列 ";
//            DataTable dt = _EluZSDataProvider.GetTable(sql);
//            if (dt.Rows.Count>0)
//            {
//                ExcelStruct excStruct = new ExcelStruct();
//                excStruct.DataTableTemp = dt;
//                excStruct.PageSource = this.Page;
//                excStruct.Flag = "0";
//                excStruct.TableName = dt.Rows[0]["值班表名"].ToString();
//                excStruct.HiddenField = "ID,值班表名";
//                DmpExcel dmpExcel = new DmpExcel();
//                dmpExcel.ExpExcel(excStruct);
//            }
//            else
//            {
//                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('该月没有值班表，请核实');</script>");
//            }
            Page.Response.Redirect("信息系统值班表"+System.DateTime.Now.Month+"月.xls");
        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            string id = string.Empty;
            if (Page.Request["Id"] == null)
            {
                string sql1 = "Select id from zs_zrb where name = '保障人员值班表" + System.DateTime.Now.Year + "年" + txt_yf.Text.Trim() + "月份'";
                id = _EluZSDataProvider.GetString(sql1);
                if (!string.IsNullOrEmpty(id))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "al", "<script>alert('该月份已有值日表！');</script>");
                    return;
                }
                else
                {
                    string sql = "insert into zs_zrb values(sys_guid(),'保障人员值班表"+System.DateTime.Now.Year+"年" + txt_yf.Text.Trim() + "月份',sysDate) ";
                    _EluZSDataProvider.RunSqlNon(sql);
                    sql1 = "Select id from zs_zrb where name = '保障人员值班表" + System.DateTime.Now.Year + "年" + txt_yf.Text.Trim() + "月份'";
                    id = _EluZSDataProvider.GetString(sql1);
                    for (int i = 1; i <= 31; i++)
                    {
                        string sql3 = "insert into zs_zrb_person(zid,序列) values('" + id + "','"+i+"') ";
                        _EluZSDataProvider.GetString(sql3);
                    }
                }
            }
            else
            {
                string sql1 = "Select id from zs_zrb where id = '"+Page.Request["ID"]+"'";
                id = _EluZSDataProvider.GetString(sql1);
            }

            gettable("null",id);
        }

        protected void gettable(string a, string id)
        {
            string sql = "select zid,name,序列,atname from zs_zrb_person where zid = '" + id + "' order by 序列  ";
            //DataTable dt = _EluZSDataProvider.GetTable(sql);
            //Repeater1.DataSource = _EluZSDataProvider.GetTable(sql);
            //Repeater1.DataBind();

            DataList1.DataSource = _EluZSDataProvider.GetTable(sql);
            DataList1.DataBind();
        }
    }
}
